﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Globalization;
using System.IO;
using System.Linq;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.UI;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;

// Pour en savoir plus sur le modèle d'élément Contrôle utilisateur, consultez la page http://go.microsoft.com/fwlink/?LinkId=234236

namespace InterfaceXAML.UserControls
{
    public sealed partial class DatePicker : UserControl
    {
        private ObservableCollection<int> _days = new ObservableCollection<int>();

        public ObservableCollection<int> Days { get { return _days; } set { _days = value; } }

        private List<String> _months = new List<String>();

        public List<String> Months { get { return _months; } set { _months = value; } }

        public DatePicker()
        {
            this.InitializeComponent();

            var today = DateTime.Now;

            Days.Clear();
            for (var i = 1; i < DateTime.DaysInMonth(today.Year, today.Month) + 1; i++)
                Days.Add(i);

            Months = CultureInfo.CurrentCulture.DateTimeFormat.MonthNames.TakeWhile(m => m != String.Empty).Select(m => m).ToList();

            var years = new List<int>();
            for (var i = 2012; i < 2113; i++)
                years.Add(i);

            Day.ItemsSource = Days;
            Month.ItemsSource = Months;
            Year.ItemsSource = years;
            
            Day.SelectedItem = today.Day;
            Month.SelectedItem = Months.ElementAt(today.Month - 1);
            Year.SelectedItem = today.Year;

            DataContext = new { Days = Days, Month = Month};
        }


        private void DatePickerChange(object sender, SelectionChangedEventArgs e)
        {
            if (Day.SelectedValue == null || Month.SelectedValue == null || Year.SelectedValue == null)
                return;

            var day = ((int)Day.SelectedItem);
            var month =((int)Month.SelectedIndex + 1);
            var year = ((int)Year.SelectedItem);

            Days.Clear();
            for(var i = 1; i < DateTime.DaysInMonth(year, month) + 1; i++)
                Days.Add(i);

            Day.SelectedIndex = 0;
        }

        public DateTime GetDate()
        {
            return new DateTime((int)Year.SelectedItem, Month.SelectedIndex + 1, Day.SelectedIndex + 1);
        }

        public void SetDate(DateTime newdate)
        {
            Year.SelectedItem = newdate.Year;
            Month.SelectedIndex = newdate.Month - 1;
            Day.SelectedIndex = newdate.Day - 1;
        }

        public void DisplayError()
        {
            WrapperError.BorderBrush = Application.Current.Resources["ErrorField"] as SolidColorBrush;
            WrapperError.BorderThickness = new Thickness(2);
            return;
        }

        public void DisplayNormal()
        {
            WrapperError.BorderBrush = new SolidColorBrush(Colors.Transparent);
            WrapperError.BorderThickness = new Thickness(0);
            return;
        }
    }
}
